<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function index(){
		$catid=M('Catid')->select();
		$this->assign('catid',$catid);
        $this->display();
    }
	public function fazhan(){
		$this->display();
	}
	/////////////////领取码获取账号///////////////////////
	public function lqm(){
		/*//		验证码检测
		$code= I('post.code');
		if(check_verify($code) === false){
			$this->error('验证码错误','index');;
		}*/
		if($_SESSION['yz']!=1){
			$this->error('不正当操作！',U('Index/index'));
		}
		$_SESSION['yz']=null;
		$catid=M('Catid')->select();
		$this->assign('catid',$catid);
		$this->display();
	}
	public function dolqm(){//领取码获取
		if(empty($_POST)){
			$this->error('内容不能为空！');
		}
		$slqm=$_POST['lqm'];
		$lqm=M('Lingquma')->where('id=1')->find();
		if($slqm!=$lqm['lingquma']){
			$this->error('领取码不正确！');
		}
		$ip=M('Ip');
		$uip=$_SERVER['REMOTE_ADDR'];
		$where=array(
				'ip'=>$uip,
				);
		$find=$ip->where($where)->find();
		/*if($find['shu']>=C('cishu')){//每天申请次数
                    $this->error('申请的次数达到上限！');
                }*/
		//如果超过24小时将该条ip记录重置为1
		$time=time();
		$time2=$find['time'];
		$shijian=$time-$time2;
		if($shijian>=86400){
			$data['shu']=1;
			$data['time']=$time;
			$del=$ip->where($where)->save($data);
		}
		$find=$ip->where($where)->find();

		if($find['shu']>=C('cishu')){//每天申请次数
			$this->error('申请的次数达到上限！');
		}elseif(empty($find['shu'])){
			$data['time']=time();
			$ip=$ip->add($data);
		}else{
			$shu=$ip->where($where)->setInc('shu',1);
		}
//随机获取数据
		$zh=M('Zhanghao');
		$count=$zh->count();
		$rand=mt_rand(1,$count);//随机
		$catid=$_SESSION['catid'];
		$where=array(
				'catid'=>$catid,
				'cishu'=>array('lt',3),
		);
		$list=$zh->where($where)->order('rand()')->find();
		if($list){
			$where=array(
					id=>$list['id'],
			);
			$zh->where($where)->setInc('cishu');
			$this->assign('zh',$list);
			$this->display();
		}else{
			$this->error('获取数据出现错误！');
		}
	}
	/////////////////验证码获取账号///////////////////////
    public function lqzh(){
//		如果该IP已经存在则条转到领取码获取页面
		//		验证码检测
		$code= I('post.code');
		if(check_verify($code) === false){
			$this->error('验证码错误','index');
		}
		$slqm=$_POST['lqm'];
		$uip=$_SERVER['REMOTE_ADDR'];
		$where=array(
				'ip'=>$uip,
		);
		$ip=M('Ip');
		$findip=$ip->where($where)->find();
		if($findip) {
			$_SESSION['yz']=1;
			$_SESSION['catid']=$_POST['catid'];
			$this->redirect("Index/lqm");
		}

//随机获取数据
		$zh=M('Zhanghao');
		$count=$zh->count();
		$rand=mt_rand(1,$count);//随机
		$where=array(
				'catid'=>$_POST['catid'],
				'cishu'=>array('lt',3),
		);
		$list=$zh->where($where)->order('rand()')->find();
		if($list){
			$where=array(
					id=>$list['id'],
			);
			$zh->where($where)->setInc('cishu');
			$data['time']=time();
			$data['ip']=$_SERVER['REMOTE_ADDR'];
			$addip=$ip->add($data);//记录IP
			$this->assign('zh',$list);
			$this->display();
		}else{
			$this->error('获取数据出现错误！');
		}
    }
}